﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PMSA.Framework.Log;

public partial class Admin_Uc_Product_EditBrand : System.Web.UI.UserControl
{
    private int brandid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["bi"] != null)
        {
            try
            {
                brandid = int.Parse(Request.QueryString["bi"].ToString());
            }
            catch
            {
                CLogManager.Write("Product.Edit.PageLoad", "brandid not a number: " + Request.QueryString["bi"].ToString(), "ADMIN");
            }
        }
        if (IsPostBack)
        {

        }
        else
        {
            GetDetailBrand(brandid);
        }
    }

    private void GetDetailBrand(int brandid)
    {
        try
        {
            Brand obj = new Brand();
            obj = obj.Detail(brandid);
            txtBrandName.Text = obj.BrandName;
            txtBrandActive.Checked = obj.IsActive == 1 ? true : false;
            logoURL.Value = obj.BrandLogo;
        }
        catch (Exception ex)
        {
            CLogManager.Write("Brand.Edit.GetDetailBrand", ex.ToString(), "ADMIN");
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            string name = txtBrandName.Text;
            int active = txtBrandActive.Checked ? 1 : 0;
            string _photoNull = "../Img/Layout/brandlogo.jpg";
            string _photoLink = "../Admin/Upload/Image/Brand/";
            string urlLogo = _photoNull;
            if (!String.IsNullOrEmpty(logoURL.Value))
            {
                urlLogo = _photoLink + logoURL.Value;
            }


            if ((new Brand().Update(brandid, name, active, urlLogo)) != null)
            {
                Response.Write("<script type='text/javascript'>alert('Edit Successful !');</script>");
            }
        }
        catch (Exception ex)
        {
            CLogManager.Write("Brand.Edit.btnEdit_Click", ex.ToString(), "ADMIN");
        }
    }
}